// pages/user/card/transferRecord/transferRecord.js

const app = getApp();
var api = require('../../../../api.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
     list:'',
     id:'',
     judge:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;

    that.setData({
      AllId:options.id
    });
    that.getNumber();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this ;
    page.getDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    // that.continueDo();
    wx.hideShareMenu();
    var url = "pages/user/card/receive/receive";
    var user_id = wx.getStorageSync('user_id');
    var id = that.data.id;
    var card = that.data.picList.gold_card_url;
    console.log(url + "?id=" + id + "&user_id=" + user_id);
    return {
      path: url + "?id=" + id + "&user_id=" + user_id,
      title: '赠送你一张1314元的黄金会员学习卡，赶快点击领取哦',
      imageUrl: card,
      success: function () {
        // that.continueDo();
      }
    };
  },

  
  
  //获取详情
  getDetail: function () {
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var page = this;
    wx.request({
      url: api.index.levleLog,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          
          page.setData({
            list:res.data.data
          })

          console.log(page.data.list)
          
        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        } else if (res.data.code == 1) {

          page.setData({
            list:''
          })

        }



      }
    });
  },
  

  // 继续转赠
  continueDo:function(res){
    console.log(res)
    var index = res.currentTarget.dataset.index
    var page = this;
    var access_token = wx.getStorageSync('access_token');

    wx.request({
      url: api.index.continueShare,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: page.data.list[index].id
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          page.setData({
            id: res.data.data,
            judge: true
          });
          console.log(page.data.id)
        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }



      }
    })
  },

  changJudge: function () {
    var page = this;
    page.setData({
      judge: false
    })
  },
  
  //获取图片
  getNumber: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    wx.request({
      url: api.card.number,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: that.data.AllId
      },
      success: function (e) {
        // console.log(e);
        var picList = e.data.data;
        // console.log(list);
        if (e.data.code == 0) {
          that.setData({
            picList: picList
          })
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },
    

  // 取消转赠
  cancelDo:function(res){
    console.log(res)
    var index = res.currentTarget.dataset.index
    var page = this;
    var access_token = wx.getStorageSync('access_token');
    
    wx.request({
      url: api.index.cancelShare,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: page.data.list[index].id
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
          page.getDetail();

        } else if (res.data.code == -1) {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        }



      }
    })
  }

})